﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using DebIt.ViewModel;
using Microsoft.Phone.Controls;

namespace DebIt.View
{
    public partial class LoanEdit
    {
        public LoanEdit()
        {
            InitializeComponent();
            LoadIcons();
        }
       
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string id;
            NavigationContext.QueryString.TryGetValue("id", out id);
            id = id == "" ? "0" : id;
            DataContext = new LoanEditViewModel(Convert.ToInt32(id));
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            ((LoanEditViewModel)DataContext).Save();
        }

        private void BindableApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void LoadIcons()
        {
            var darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                imgAdd.Source = new BitmapImage(new Uri("../icons/dark/add.png", UriKind.Relative));
            }
            else
            {
                imgAdd.Source = new BitmapImage(new Uri("../icons/light/add.png", UriKind.Relative));
            }
        }

        private void ImgDeleteLoaded(object sender, RoutedEventArgs e)
        {
            var img = (Image) sender;
            var darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                img.Source = new BitmapImage(new Uri("../icons/dark/delete.png", UriKind.Relative));
            }
            else
            {
                img.Source = new BitmapImage(new Uri("../icons/light/delete.png", UriKind.Relative));
            }
        }
    }
}